package com.thread.threadfirst.rabbitMQPublisher.publisher;

import com.thread.threadfirst.rabbitMQPublisher.constant.AMQPConstant;
import com.thread.threadfirst.rabbitMQPublisher.vo.RegisterVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.MessagePostProcessor;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.amqp.RabbitProperties;
import org.springframework.boot.autoconfigure.jms.activemq.ActiveMQProperties;
import org.springframework.stereotype.Component;
import sun.rmi.runtime.Log;

import java.util.Date;
import java.util.UUID;

/**
 * 死信队列生产者
 */
@Component
@Slf4j
public class DLXPublisher {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    public void send(int count){
        for (int i=0;i<count;i++){
            RegisterVO registerVO=new RegisterVO();
            registerVO.setUserId(9528L);
            registerVO.setPhone(UUID.randomUUID().toString());
            registerVO.setRegisterTime(new Date());
            rabbitTemplate.setMessageConverter(new Jackson2JsonMessageConverter());
            //
            MessagePostProcessor messagePostProcessor=message -> {
                MessageProperties messageProperties=message.getMessageProperties();
                messageProperties.setExpiration("10000");
                return message;
            };
            rabbitTemplate.convertAndSend(AMQPConstant.EXCHANGE_TOPIC_FIRST,"dl_k",registerVO,messagePostProcessor);
            log.info("send message:{},{}",i,registerVO);
        }
        log.info("success");
    }
}
